﻿using CSRedis;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SX.Core
{
    /// <summary>
    /// Redis缓存
    /// </summary>
    public class RedisCache : ICache
    {
        public RedisCache()
        {
            var redis = new CSRedis.CSRedisClient(ConfigHelper.Get("RedisConnStr"));
            //RedisHelper帮助类
            RedisHelper.Initialization(redis);
        }
        public long Remove(params string[] key)
        {
            return RedisHelper.Del(key);
        }

        public Task<long> RemoveAsync(params string[] key)
        {
            return RedisHelper.DelAsync(key);
        }
        public async Task<long> RemoveByPatternAsync(string pattern)
        {
            if (pattern.IsNull())
                return default;

            pattern = Regex.Replace(pattern, @"\{.*\}", "*");

            var keys = (await RedisHelper.KeysAsync(pattern));
            if(keys != null && keys.Length > 0)
            {
                return await RedisHelper.DelAsync(keys);
            }

            return default;
        }

        public bool Exists(string key)
        {
            return RedisHelper.Exists(key);
        }

        public Task<bool> ExistsAsync(string key)
        {
            return RedisHelper.ExistsAsync(key);
        }

        public string Get(string key)
        {
            return RedisHelper.Get(key);
        }

        public T Get<T>(string key)
        {
            return RedisHelper.Get<T>(key);
        }

        public Task<string> GetAsync(string key)
        {
            return RedisHelper.GetAsync(key);
        }

        public Task<T> GetAsync<T>(string key)
        {
            return RedisHelper.GetAsync<T>(key);
        }

        public bool Set(string key, object value)
        {
            return RedisHelper.Set(key, value);
        }

        public bool Set(string key, object value, TimeSpan expire)
        {
            return RedisHelper.Set(key, value, expire);
        }

        public Task<bool> SetAsync(string key, object value)
        {
            return RedisHelper.SetAsync(key, value);
        }

        public Task<bool> SetAsync(string key, object value, TimeSpan expire)
        {
            return RedisHelper.SetAsync(key, value, expire);
        }

        public Task<bool> SetAsync(string key, object value, double expirationMinute)
        {
            TimeSpan ts = DateTime.Now.AddMinutes(expirationMinute) - DateTime.Now;
            return RedisHelper.SetAsync(key, value,ts);
        }

        public Task<bool> SetAsync(string key, object value, DateTime dateTime)
        {
            TimeSpan ts = dateTime - DateTime.Now;
            return RedisHelper.SetAsync(key, value, ts);

            //RedisHelper.ExpireAtAsync(key, dateTime);
        }

        public bool TryGetValue<T>(string key, out T value)
        {
            value = default;
            if (Exists(key))
            {
                value = Get<T>(key);
                return true;
            }
            return false;
        }
    }
}
